package com.example.myapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.user.User;

import org.litepal.LitePal;

import java.util.List;

public class loginActivity extends AppCompatActivity {
    private EditText usnedit,psdedit;
    private Button loginbtn,registerbtn;
    private RadioGroup radioGroup;
    int CHOOSE_LEVEL=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        User user=new User(0,"admin","admin",2);
        initView();
        loginView();

    }

    private void loginView() {
        //选择监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.userrdb:
                        CHOOSE_LEVEL=1;
                        break;
                    case R.id.adminrdb:
                        CHOOSE_LEVEL=2;
                        break;
                }
            }
        });

        //事件监听
        loginbtn.setOnClickListener(new View.OnClickListener() {//登录按钮
            @Override
            public void onClick(View v) {
                //账号密码添加
                String usn=usnedit.getText().toString().trim();
                String psd=psdedit.getText().toString().trim();
                User user=confirmUser(usn,psd);

                if(user==null){//==null说明没有添加注册
                    showToast("账号密码错误");
                }else if (user.getPermission()==CHOOSE_LEVEL){
                    SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();//记录数据
                    //登录成功就存储下三列数据
                    editor.putInt("userid",user.getId());
                    editor.putString("username",user.getName());
                    editor.putString("password",user.getPassword());
                    editor.putInt("permission",user.getPermission());
                    editor.apply();//提交事务
                    Intent intent=new Intent(loginActivity.this,MainActivity.class);
                    startActivity(intent);
                }else {
                    showToast("权限选择错误");
                }
            }
        });
        registerbtn.setOnClickListener(new View.OnClickListener() {//注册按钮
            @Override
            public void onClick(View v) {
                //添加注册账号和密码
                String usn=usnedit.getText().toString().trim();
                String psd=psdedit.getText().toString().trim();

                User user=new User(0,usn,psd);
                //判断已添加的账号密码是否重复
                if(user.save()){
                    showToast("注册成功");
                }else {
                    showToast("该用户已存在，请登录");
                }
            }
        });


    }
    //有参吐丝方法
    private void showToast(String str){
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    private User confirmUser(String usn,String psd){//数据库内容调用

        List<User> userList= LitePal.findAll(User.class);
        for(User u:userList){//数据对比成功返回u，不成功返回空
            if (usn.equals(u.getName())&&psd.equals(u.getPassword())){
                return u;
            }
        }
        return null;
    }

    private void initView() {
        loginbtn=findViewById(R.id.btn_my_login);
        registerbtn=findViewById(R.id.btn_my_reg);
        usnedit=findViewById(R.id.et_my_name);
        psdedit=findViewById(R.id.et_my_pwd);
        radioGroup=findViewById(R.id.radiogroup);
    }
}
